package web.suzy.oj.dao.discussion.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import web.suzy.oj.dao.discussion.CommentEntityService;
import web.suzy.oj.dao.discussion.DiscussionEntityService;
import web.suzy.oj.dao.msg.MsgRemindEntityService;
import web.suzy.oj.dao.user.UserInfoEntityService;
import web.suzy.oj.mapper.CommentMapper;
import web.suzy.oj.pojo.entity.discussion.Comment;
import web.suzy.oj.pojo.entity.discussion.Discussion;
import web.suzy.oj.pojo.entity.msg.MsgRemind;
import web.suzy.oj.pojo.vo.CommentVO;

import javax.annotation.Resource;
import java.util.List;

/**
 * YangSuzy 软件工作室
 * 类名: CommentEntityServiceImpl
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/11/27 15:54
 */
@Service
public class CommentEntityServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentEntityService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private DiscussionEntityService discussionEntityService;

    @Resource
    private MsgRemindEntityService msgRemindEntityService;

    /**
     * 方法名: getCommentList
     * 描述: 获取评论列表
     *
     * @param limit       每页显示数目
     * @param currentPage 当前页
     * @param did         讨论ID
     * @return com.baomidou.mybatisplus.core.metadata.IPage<web.suzy.oj.pojo.vo.CommentVO>
     * @date 2022/11/27 15:54
     * @auther YangSuzy
     **/
    @Override
    public IPage<CommentVO> getCommentList(int limit, int currentPage, Integer did) {
        //新建分页
        Page<CommentVO> page = new Page<>(currentPage, limit);
        return commentMapper.getCommentList(page, did);
    }

    /**
     * 方法名: updateCommentMsg
     * 描述: 异步方法 - 更新评论消息
     *
     * @param recipientId  接收方ID
     * @param senderId     发送方ID
     * @param content      消息内容
     * @param discussionId 讨论ID
     * @return void
     * @date 2022/11/27 15:59
     * @auther YangSuzy
     **/
    @Async
    @Override
    public void updateCommentMsg(String recipientId, String senderId, String content, Integer discussionId) {
        //超出部分转为省略号
        if (content.length() > 200) {
            content = content.substring(0, 200) + "...";
        }
        //新建消息提醒对象
        MsgRemind msgRemind = new MsgRemind();
        //设置操作类型：讨论
        msgRemind.setAction("Discuss")
                //设置接收方ID
                .setRecipientId(recipientId)
                //设置发送方ID
                .setSenderId(senderId)
                //设置消息内容
                .setSourceContent(content)
                //设置消息来源：被评论的讨论ID
                .setSourceId(discussionId)
                //设置事件源类型：讨论
                .setSourceType("Discussion")
                //设置发生操作的url
                .setUrl("/discussion-detail/" + discussionId);
        //保存消息提示
        msgRemindEntityService.saveOrUpdate(msgRemind);
    }

    /**
     * 方法名: updateCommentLikeMsg
     * 描述: 异步方法 - 更新评论的点赞消息
     *
     * @param recipientId  接收方ID
     * @param senderId     发送方ID
     * @param discussionId 讨论ID
     * @return void
     * @date 2022/11/27 16:05
     * @auther YangSuzy
     **/
    @Async
    @Override
    public void updateCommentLikeMsg(String recipientId, String senderId, Integer discussionId) {
        //新建消息提醒对象
        MsgRemind msgRemind = new MsgRemind();
        //设置操作类型：点赞讨论
        msgRemind.setAction("Like_Discuss")
                //设置接收方ID
                .setRecipientId(recipientId)
                //设置发送方ID
                .setSenderId(senderId)
                //设置消息来源
                .setSourceId(discussionId)
                //设置消息类型
                .setSourceType("Discussion");
        Discussion discussion = discussionEntityService.getById(discussionId);
        //设置发生操作的url
        if (discussion != null) {
            msgRemind.setUrl("/discussion-detail/" + discussionId);
        }
        msgRemindEntityService.saveOrUpdate(msgRemind);
    }
}
